﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Hutech.Authentication.Library.Data;


namespace Hutech.Authentication.Library.Business
{
    public class Actions
    {

        #region Constructors

        public Actions(){ }


        public Actions(Guid actionGuid)
        {
            GetAction(actionGuid);
        }

        #endregion

        #region Private Properties

        private Guid actionKindGuid = Guid.Empty;
        private string actionKindName = string.Empty;
        private Guid actionGuid = Guid.Empty;
        private string actionName = string.Empty;
        private string description = string.Empty;
        private string actionCommand = string.Empty;
        
        //Add by tsandtm
        private bool isSelect = false;
        #endregion

        #region Public Properties
        public bool IsSelect
        {
            get { return isSelect; }
            set { isSelect = value; }
        }
        public Guid ActionKindGuid
        {
            get { return actionKindGuid; }
            set { actionKindGuid = value; }
        }
        public string ActionKindName
        {
            get { return actionKindName; }
            set { actionKindName = value; }
        }
        public Guid ActionGuid
        {
            get { return actionGuid; }
            set { actionGuid = value; }
        }
        public string ActionName
        {
            get { return actionName; }
            set { actionName = value; }
        }
        public string Description
        {
            get { return description; }
            set { description = value; }
        }
        public string ActionCommand
        {
            get { return actionCommand; }
            set { actionCommand = value; }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of Action.
        /// </summary>
        /// <param name="actionGuid"> actionGuid </param>
        private void GetAction(
            Guid actionGuid)
        {
            using (IDataReader reader = DBActions.GetOne(
                actionGuid))
            {
                PopulateFromReader(reader);
            }

        }


        private void PopulateFromReader(IDataReader reader)
        {
            if (reader.Read())
            {
                this.actionKindGuid = new Guid(reader["ActionKindGuid"].ToString());
                this.actionKindName = reader["ActionKindName"].ToString();
                this.actionGuid = new Guid(reader["ActionGuid"].ToString());
                this.actionName = reader["ActionName"].ToString();
                this.description = reader["Description"].ToString();
                this.actionCommand = reader["ActionCommand"].ToString();

            }

        }

        /// <summary>
        /// Persists a new instance of Action. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create()
        {
            this.actionGuid = Guid.NewGuid();

            int rowsAffected = DBActions.Create(
                this.actionKindGuid,
                this.actionKindName,
                this.actionGuid,
                this.actionName,
                this.description,
                this.actionCommand);

            return (rowsAffected > 0);

        }


        /// <summary>
        /// Updates this instance of Action. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update()
        {

            return DBActions.Update(
                this.actionKindGuid,
                this.actionGuid,
                this.actionKindName,
                this.actionName,
                this.description,
                this.actionCommand);

        }





        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance of Action. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save()
        {
            if (this.actionGuid != Guid.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
        }




        #endregion

        #region Static Methods

        /// <summary>
        /// Deletes an instance of Action. Returns true on success.
        /// </summary>
        /// <param name="actionGuid"> actionGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(
                Guid actionGuid)
        {
            return DBActions.Delete(
                actionGuid);
        }


        /// <summary>
        /// Gets a count of Action. 
        /// </summary>
        public static int GetCount()
        {
            return DBActions.GetCount();
        }

        private static List<Actions> LoadListFromReader(IDataReader reader)
        {
            List<Actions> actionList = new List<Actions>();
            try
            {
                while (reader.Read())
                {
                    Actions action = new Actions();
                    action.actionKindGuid = new Guid(reader["ActionKindGuid"].ToString());
                    action.actionKindName = reader["ActionKindName"].ToString();
                    action.actionGuid = new Guid(reader["ActionGuid"].ToString());
                    action.actionName = reader["ActionName"].ToString();
                    action.description = reader["Description"].ToString();
                    action.actionCommand = reader["ActionCommand"].ToString();
                    actionList.Add(action);

                }
            }
            finally
            {
                reader.Close();
            }

            return actionList;

        }

        /// <summary>
        /// Gets an IList with all instances of Action.
        /// </summary>
        public static List<Actions> GetAll()
        {
            IDataReader reader = DBActions.GetAll();
            return LoadListFromReader(reader);

        }

        /// <summary>
        /// Gets an IList with page of instances of Action.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static List<Actions> GetPage(int pageNumber, int pageSize, out int totalPages)
        {
            totalPages = 1;
            IDataReader reader = DBActions.GetPage(pageNumber, pageSize, out totalPages);
            return LoadListFromReader(reader);
        }



        #endregion

        #region Static method
        private static List<Actions> LoadListFromReaderKindAction(IDataReader reader)
        {
            List<Actions> actionList = new List<Actions>();
            try
            {
                while (reader.Read())
                {
                    Actions action = new Actions();
                    action.actionKindGuid = new Guid(reader["ActionKindGuid"].ToString());
                    action.actionKindName = reader["ActionKindName"].ToString();
                    actionList.Add(action);

                }
            }
            finally
            {
                reader.Close();
            }

            return actionList;

        }

        /// <summary>
        /// Get all Kind Action on table Actions
        /// </summary>
        /// <returns></returns>
        public static List<Actions> GetAllKindAction()
        {
            IDataReader reader = DBActions.GetAllKindAction();
            return LoadListFromReaderKindAction(reader);
        }
        #endregion
    }

}
